﻿using System;
using System.Collections.Specialized;

using FireBus.Contracts;
using FireBus.Core;
using FireBus.Web.Contracts;


namespace FireBus.Web.Framework
{
	public class ExtensionAction
	{
		private readonly ExtensionName _ExtensionName;

		private readonly String _Action;


		public ExtensionName ExtensionName
		{
			get { return _ExtensionName; }
		}

		public String Action
		{
			get { return _Action; }
		}


		public ExtensionAction(ExtensionName extensionName, String action)
		{
			if (extensionName == null)
			{
				throw new ArgumentNullException("extensionName");
			}
			if (String.IsNullOrEmpty(action))
			{
				throw new ArgumentNullException("action");
			}
			_ExtensionName = extensionName;
			_Action = action;
		}

		public HttpResponse Execute(Host host, NameValueCollection parameters)
		{
			if (host == null)
			{
				throw new ArgumentNullException("host");
			}
			var request = new HttpRequest(_Action, parameters);
			var asyncResult = host.BeginRequest<HttpRequest, HttpResponse>(
				_ExtensionName, request, null, null);
			var response = host.EndRequest<HttpResponse>(asyncResult);
			return response;
		}
	}
}
